package com.stox.ui.javafx.indicator.view;

import java.util.Collection;
import java.util.List;
import java.util.Locale;

import javafx.geometry.Bounds;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Screen;
import javafx.util.Callback;
import lombok.Delegate;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import com.stox.dto.AbstractModel;
import com.stox.dto.IndicatorModel;
import com.stox.dto.intf.AbstractModelNavigable;
import com.stox.indicator.Indicator;
import com.stox.ui.javafx.core.model.TreeViewAbstractModelSearchable;
import com.stox.ui.javafx.core.view.View;
import com.stox.ui.javafx.core.widget.AbstractModelSearchPanel;
import com.stox.ui.javafx.core.widget.AbstractModelTreeCell;
import com.stox.ui.javafx.core.widget.AbstractModelTreeView;
import com.stox.ui.javafx.core.widget.StoxStage;

@Component
public class IndicatorView extends View implements InitializingBean, AbstractModelNavigable<List<AbstractModel>>{
	private static final long serialVersionUID = -7123253881754094862L;

	@Autowired
	private ApplicationContext applicationContext;
	
	@Autowired
	private AbstractModelSearchPanel abstractModelSearchPanel;
	
	@Autowired
	private AbstractModelTreeView treeView;
	
	@Delegate
	private final TreeViewAbstractModelSearchable treeViewabstractModelSearchable = new TreeViewAbstractModelSearchable();
	
	private BorderPane contentPane = new BorderPane();
	
	@Override
	@SuppressWarnings({"serial","rawtypes"})
	public void afterPropertiesSet() throws Exception {
		setTitle(applicationContext.getMessage("application.name.indicator", null, Locale.getDefault()));
		
		treeView.setRoot(new TreeItem<AbstractModel>(new AbstractModel(AbstractModel.Type.ANY){}));
		treeView.setShowRoot(false);
		treeView.getRoot().setExpanded(true);
		treeView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
		treeView.setCellFactory(new Callback<TreeView<AbstractModel>, TreeCell<AbstractModel>>() {
			@Override
			public TreeCell<AbstractModel> call(TreeView<AbstractModel> param) {
				return new AbstractModelTreeCell();
			}
		});
		Collection<Indicator> indicators = applicationContext.getBeansOfType(Indicator.class).values();
		for(Indicator indicator : indicators){
			treeView.getRoot().getChildren().add(new TreeItem<AbstractModel>(new IndicatorModel(indicator)));
		}
		contentPane.setCenter(treeView);
		
		treeViewabstractModelSearchable.setTreeView(treeView);
		
		abstractModelSearchPanel.setabstractModelSearchable(treeViewabstractModelSearchable);
		contentPane.setBottom(abstractModelSearchPanel);
		getContentPane().getChildren().add(contentPane);
	}
	
	private void setPosition(){
		Bounds bounds = StoxStage.getInstance().getContentPane().getBoundsInParent();
		setLayoutX(bounds.getMaxX() - getPrefWidth());
		setLayoutY(bounds.getMinY());
	}
	
	@Override
	public void open() {
		setPrefWidth(Screen.getPrimary().getVisualBounds().getWidth()/5);
		setPrefHeight(StoxStage.getInstance().getContentPane().getBoundsInLocal().getHeight()/2);
		setPosition();
		super.open();
	}
	
}
